package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.pojo.TaotaoResult;
import com.mall.mapper.TbItemParamMapper;
import com.mall.pojo.EUDataGridResult;
import com.mall.pojo.TbItemParam;
import com.mall.pojo.TbItemParamExample;
import com.mall.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ItemParamServiceImpl implements ItemParamService{

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Override
    public TaotaoResult getItemParamByCid(long cid) {

        TbItemParamExample example=new TbItemParamExample();

        TbItemParamExample.Criteria criteria=example.createCriteria();

        criteria.andItemCatIdEqualTo(cid);

        List<TbItemParam> list= tbItemParamMapper.selectByExampleWithBLOBs(example);

        if(list!=null && list.size()>0){
            return TaotaoResult.ok(list.get(0));
        }

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult insertItemParam(TbItemParam tbItemParam) {


        tbItemParam.setCreated(new Date());
        tbItemParam.setUpdated(new Date());

        tbItemParamMapper.insert(tbItemParam);

        return TaotaoResult.ok();
    }

    @Override
    public EUDataGridResult getItemParamList(int page, int rows) {

        TbItemParamExample tbItemParamExample=new TbItemParamExample();

        PageHelper.startPage(page,rows);
        List<TbItemParam> list=tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);

        PageInfo<TbItemParam> itemParamPageInfo=new PageInfo<>(list);

        long total=itemParamPageInfo.getTotal();

        EUDataGridResult result=new EUDataGridResult();
        result.setTotal(total);
        result.setRows(list);

        return result;
    }
}
